package com.mx.mg.idempotence.controller;

import com.mx.mg.idempotence.common.ServerResponse;
import com.mx.mg.idempotence.common.annotation.ApiIdempotent;
import com.mx.mg.idempotence.service.TestService;
import com.mx.mg.idempotence.service.TokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: ghm
 * @Date: 2021/09/03/14:01
 * @Description:
 */
@Api(tags = "幂等性验证")
@RestController
@RequestMapping("/api")
public class BusinessController {

    @Autowired
    private TokenService tokenService;


    @Autowired
    private TestService testService;

    @ApiOperation("获取token")
    @PostMapping("get/token")
    public ServerResponse getToken() {
        String token = tokenService.createToken();
        if (StringUtils.isNotEmpty(token)) {
            return ServerResponse.success(token);
        }
        return ServerResponse.error("token为空");
    }


    @ApiOperation("验证消息")
    @ApiImplicitParam(name = "token",required = false ,paramType = "header" ,dataType = "String")
    @ApiIdempotent
    @PostMapping("/test/Idempotence")
    public ServerResponse cheakIdempotence() {
        return testService.testIdempotence();
    }
}

